//
//  JPNavigationController.swift
//  JPWeibo
//
//  Created by 哇咔咔 on 16/9/19.
//  Copyright © 2016年 jp. All rights reserved.
//

import UIKit

class JPNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
        
    }
    
    //  重写该方法监听push的子视图控制及导航视图控制器里面有几个子视图控制器
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            //  表示当前是第二个视图控制器
            if viewControllers.count == 1 {
                
                //  获取上一级控制的title
                let title = viewControllers.first!.title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            
            } else {
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }
            
            viewController.title = "当前显示的第\(viewControllers.count + 1)级控制器"
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    
    func backAction()  {
        
        popViewController(animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
